package ui;

import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import fast.*;

/**
 * Class for a graphical user interface for a slave node
 * 
 * @author Adam Bell
 *
 */
public class SlaveUI extends JFrame implements ActionListener{
	
	SlaveProcessor sp = null;
	
	/**
	 * Constructor 
	 */
	public SlaveUI() throws FileNotFoundException, IOException{
		sp = new SlaveProcessor();
		sp.start();
	}
	
	/**
	 * Places the UI on screen.
	 * 
	 */
	public void showUI(){
		JFrame frame = new JFrame("");
		JPanel defaultPanel = new JPanel();
		
		defaultPanel.setLayout(new BoxLayout(defaultPanel, BoxLayout.Y_AXIS));
		frame.setContentPane(defaultPanel);
		
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setSize(400, 150);
		frame.setResizable(false);

		JPanel p = new JPanel();
		p.setBorder(BorderFactory.createTitledBorder("FastDDF Slave Processor"));
		p.setLayout(new GridLayout(2,1));
		defaultPanel.add(p);

		JButton leaveBTN = new JButton("Disconnect from Master");
		leaveBTN.setActionCommand("leave");
		leaveBTN.addActionListener(this);
		p.add(leaveBTN);
		
		JButton quitBTN = new JButton("Shutdown this Slave node");
		quitBTN.setActionCommand("quit");
		quitBTN.addActionListener(this);
		p.add(quitBTN);
		
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	
	}
	
	/**
	 * Monitor the UI for click events
	 */
	public void actionPerformed(ActionEvent e) {
		
		String action = e.getActionCommand();
		
		if(action.equals("leave")){
			sp.sysExit();
		} else if(action.equals("quit")){
			sp.sysShut();
		}
		
	}
	
	/**
	 * Main method to launch the UI with
	 * 
	 * @param args
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException{
		
		SlaveUI ui = new SlaveUI();
		
		ui.showUI();
		
	}


}
